//N叉树的层序遍历
class Node {
public:
    int val;
    vector<Node*> children;

    Node() {}

    Node(int _val) {
        val = _val;
    }

    Node(int _val, vector<Node*> _children) {
        val = _val;
        children = _children;
    }
};
*/

class Solution {
public:
    vector<vector<int>> ret;
    void bfs(queue<Node*>& q)
    {
        while(!q.empty())
        {
            vector<int> temp;
            //遍历一层
            int size = q.size();
            while(size--)
            {
                Node* node = q.front();
                temp.push_back(node->val);
                q.pop();
                //带出这个节点连接的剩下节点
                for(auto& child : node->children)
                {
                    q.push(child);
                }
            }
            //遍历完一层以后把这层的节点插入到ret中
            ret.push_back(temp);
        }
    }
    vector<vector<int>> levelOrder(Node* root) {
        if(root == nullptr) return ret;
        queue<Node*> q;
        q.push(root);
        bfs(q);
        return ret;
    }
};

//二叉树的锯齿形层序遍历
class Solution {
public:
    vector<vector<int>> ret;
    void bfs(queue<TreeNode*>& q)
    {
        bool ltor = true;
        while(!q.empty())
        {
            vector<int> temp;
            int size = q.size();
            while(size--)
            {
                TreeNode* node = q.front();
                q.pop();
                temp.push_back(node->val);
                if(node->left) q.push(node->left);
                if(node->right) q.push(node->right);
            }
            if(!ltor) reverse(temp.begin(),temp.end());
            ret.push_back(temp);
            ltor = !ltor;
        }
    }
    vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
        if(root == nullptr) return ret;
        queue<TreeNode*> q;
        q.push(root);
        bfs(q);
        return ret;
    }
};

//二叉树的最大宽度
class Solution {
public:
    size_t bfs(queue<pair<TreeNode*,size_t>>& q)
    {
        size_t ret = 0;
        while(!q.empty())
        {
            //层序遍历
            int size = q.size();
            pair<TreeNode*,size_t> front = q.front();
            pair<TreeNode*,size_t> back = q.back();
            ret = max(ret,back.second-front.second + 1);
            while(size--)
            {
                TreeNode* node = q.front().first;
                size_t num = q.front().second;
                q.pop();
                if(node->left) q.push({node->left,2*num});
                if(node->right) q.push({node->right,2*num+1});
            }
        }
        return ret;
    }
    int widthOfBinaryTree(TreeNode* root) {
        queue<pair<TreeNode*,size_t>> q;
        q.push({root,1});
        return bfs(q);
    }
};

//在每个树行中找最大值
class Solution {
public:
    vector<int> ret;
    void bfs(queue<TreeNode*> q)
    {
        while(!q.empty())
        {
            int size = q.size();
            int maxNum = INT_MIN;
            while(size--)
            {
                TreeNode* node = q.front();
                maxNum = max(maxNum,node->val);
                q.pop();
                if(node->left) q.push(node->left);
                if(node->right) q.push(node->right);
            }
            ret.push_back(maxNum);
        }
    }
    vector<int> largestValues(TreeNode* root) {
        if(root == nullptr) return ret;
        queue<TreeNode*> q;
        q.push(root);
        bfs(q);
        return ret;
    }
};